package com.indexingsystem.util;

import com.indexingsystem.entity.ExportProductIndexingVo;
import com.indexingsystem.entity.RelationThemeWord;
import com.indexingsystem.entity.ThemeTable;
import com.indexingsystem.entity.ThemeWord;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class XmlUtil {


    public  void exportIndexing(ExportProductIndexingVo vo) throws FileNotFoundException, UnsupportedEncodingException {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateStr = df.format(new Date());
        String exportPath = "E:/home/hadoop/test" + dateStr + ".xml";
        Document document = DocumentHelper.createDocument();

        Element root = document.addElement("Root");
        Element products = root.addElement("Products");
        products.addElement("productid").setText(vo.getProductId());
        products.addElement("name").setText(vo.getProductName());
        products.addElement("exportTime").setText(df.format(vo.getExportTime()));
        products.addElement("exportTime").setText(vo.getExportType());
        Element tables = root.addElement("ThemeTables");

        List<ThemeTable> voTables = vo.getTables();
        if (voTables != null && voTables.size() > 0) {
            for (ThemeTable t : voTables) {
                addThemeTalbeElement(tables,t);
            }
        }

        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");
        XMLWriter writer = new XMLWriter(new FileOutputStream(exportPath),format);
        try {
            writer.write(document);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public  void addThemeTalbeElement(Element parent, ThemeTable child) {
        Element element = parent.addElement("ThemeTable");
        element.addAttribute("id", String.valueOf(child.getThemeTableId()));
        element.addAttribute("name", child.getThemeTableName());
        element.addAttribute("code", child.getThemeTableCode());
        if (child.getThemeWords() != null && child.getThemeWords().size() > 0) {
            Element relationThemeWords = element.addElement("ThemeWords");
            for (ThemeWord t : child.getThemeWords()) {
                addThemeWordElement(relationThemeWords,t);
            }
        }
    }

    public  void addThemeWordElement(Element parent, ThemeWord child) {
        Element element = parent.addElement("ThemeWord");
        element.addAttribute("id", String.valueOf(child.getThemeWordId()));
        element.addAttribute("name", child.getThemeWordName());
        element.addAttribute("code", child.getThemeWordCode());
        if (child.getRelationThemeWords() != null && child.getRelationThemeWords().size() > 0) {
            Element relationThemeWords = element.addElement("RelationThemeWords");
            for (RelationThemeWord r : child.getRelationThemeWords()) {
                addRelationThemeWordElement(relationThemeWords,r);
            }
        }
    }

    public static void addRelationThemeWordElement(Element parent, RelationThemeWord child) {
        Element element = parent.addElement("RelationThemeWord");
        element.addAttribute("id", String.valueOf(child.getThemeWordId()));
        element.addAttribute("name", child.getThemeWordName());
        element.addAttribute("code", child.getThemeWordCode());
    }

}
